<%-- 
    Document   : index
    Created on : Dec 7, 2013, 10:19:36 AM
    Author     : Yiku
--%>

<%@page import="model.PointModel"%>
<%@page import="model.GraphModel"%>
<%@page import="servlets.PricerCTRLServlet"%>
<%@page import="java.util.logging.*"%>
<%@page import="java.io.*"%>
<%@page import="java.util.*"%>
<%@page import="model.Algorithm.OptionType"%>
<%@page import="model.Algorithm"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Pricer</title>
        <script type="text/javascript" src="https://www.google.com/jsapi"></script>
        <script type="text/javascript">
            google.load("visualization", "1", {packages:["corechart"]});
            google.setOnLoadCallback(drawChart);
            function drawChart() {
<%
        ArrayList<Algorithm> algorithms = (ArrayList<Algorithm>)request.getAttribute("algorithms");
        int graphCount = 1;
        for(Algorithm algorithm:algorithms){
            if(request.getAttribute(algorithm.getAlgoName()+"-GraphModel") == null){
                continue;
            }
            GraphModel graphModel = (GraphModel) request.getAttribute(algorithm.getAlgoName()+"-GraphModel");
%>
              
              var data<%=graphCount%> = google.visualization.arrayToDataTable([
                  ['Volatility', 'Price'],
<%
            for(PointModel pointModel:graphModel.getPointsArray()){
                out.println("['"+pointModel.getPrice()+"', " + pointModel.getVolatility() +"],");          
              }
%>
            ]);
            var options<%=graphCount%> = {
              title: '<%=graphModel.getName()%>'
            };

            var chart = new google.visualization.LineChart(document.getElementById('<%=algorithm.getAlgoName()%>'));
            chart.draw(data<%=graphCount%>, options<%=graphCount%>);
    <%graphCount++;
        }%>          }

        </script>
    </head>
    <body>
        <h1>Option Pricer</h1>
        <form action="PricerCTRLServlet">
            
            <%-- Table 1 shows input data of an option --%>
            <table border="0" width="0">
                <thead>
                    <tr>
                        <th>Please input the following information:</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td>Company Ticker</td>
                        <td><input type="text" name="companyTicker" value="" /></td>
                    </tr>
                    <tr>
                        <td>Current Stock Price</td>
                        <td><input type="text" name="currentStockPrice" value="50" /></td>
                    </tr>
                    <tr>
                        <td>Strike Price</td>
                        <td><input type="text" name="strikePrice" value="40" /></td>
                    </tr>
                    <tr>
                        <td>Term (in year)</td>
                        <td><input type="text" name="term" value="0.4" /></td>
                    </tr>
                    <tr>
                        <td>Risk Free Rate</td>
                        <td><input type="text" name="riskFreeRate" value="0.1" /></td>
                    </tr>
                    <tr>
                        <td>Volatility</td>
                        <td><input type="text" name="volatility" value="0.4" /></td>
                    </tr>
                </tbody>
            </table>
            
            <%-- Table 2 shows input data of algo & graph, the forth & fifth column should load additional input --%>
            <table border="1">
                <thead>
                    <tr>
                        <th>Use</th>
                        <th>Algorithm Name</th>
                        <th>Option Type</th>
                        <th>Display Volatility Smile</th>
                        <th>Additional Input</th>
                    </tr>
                </thead>
                <tbody>
                    <%
                        for(Algorithm algorithm:algorithms){
                    %>
                    <tr>
                        <td><input type="checkbox" name="use-<%=algorithm.getAlgoName()%>" value="ON"/></td>
                        <td><%=algorithm.getAlgoName()%></td>
                        <td>
                            <select name="ddl-<%=algorithm.getAlgoName()%>">
                                <%for(OptionType optionType:algorithm.getOptionTypes()){%>
                                    <option><%=optionType%></option> 
                                <%}%>
                            </select>
                        </td>
                        <td><input type="checkbox" name="smile-<%=algorithm.getAlgoName()%>" value="ON" /></td>
                        <td>
                     <%
                             HashMap<String,Double> eP = algorithm.getExtraParameters();
                             if(eP!=null){
                                 for(Map.Entry<String,Double> entry : eP.entrySet()){%>
                                    <%=entry.getKey()%><input  type = "text" name = "<%out.print(algorithm.getAlgoName()+"-"+entry.getKey());%>" value = "" />
                               <%}
                             }
                             
                             %>
                        </td>
                    <%
                        }
                    %>
                    </tr>
                </tbody>
            </table>
                    <input type="submit" value="Calculate" name="calculate" />
        </form>
                                
                    <table>
                        <%
                        for(Algorithm algorithm:algorithms){
                            if(request.getAttribute(algorithm.getAlgoName()+"-Result") == null){
                                continue;
                            }
                        %> 
                        <tr>
                            <td>
                                <%=algorithm.getAlgoName()+" Results: "%>
                            </td>
                            <td>
                                <%=request.getAttribute(algorithm.getAlgoName()+"-Result")%>
                            </td>
                        </tr>
                        <%}%>
                    </table>         
                            
<%
        for(Algorithm algorithm:algorithms){
            if(request.getAttribute(algorithm.getAlgoName()+"-GraphModel") == null){
                continue;
            }
            %><div id="<%=algorithm.getAlgoName()%>" style="width: 900px; height: 500px;"></div><p></p><%
        }
%>        
        <div>
            <h3> Choose File to Upload in Server </h3>
            **Warning** Uploading invalid files might affect system stability
            <form action="AddAlgoCTRLServlet" method="post" enctype="multipart/form-data">
                <input type="file" name="file" />
                <input type="submit" value="upload" />
            </form>
            <%if(request.getAttribute("uploadSuccess")!=null){%>
                Upload Successful!
            <%}%>
            
        </div>
    </body>
</html>